<?php

namespace zhtfrontend\models\datas;

use common\components\extenders\MenuManage;
use Yii;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;
use yii\helpers\ArrayHelper;
use doumiaoduo\ueditor\ImageUploadMultipleTrait2;

/**
 * This is the model class for table "{{%mechanic_settlement}}".
 *
 * @property integer $id
 * @property integer $mechanic_id
 * @property integer $status
 * @property string $settlement_amount
 * @property string $commission_money
 * @property string $payment_method
 * @property integer $payment_id
 * @property string $operator
 * @property integer $operator_id
 * @property integer $payment_time
 * @property integer $type
 * @property integer $created_at
 * @property integer $subsidy
 * @property integer $year
 * @property integer $month
 * @property integer $total
 * @property integer $subsidy_money
 */
class MechanicSettlement extends \zhtfrontend\components\bases\BaseModel
{
    use ImageUploadMultipleTrait2;
    public $pic_url;
    public $picture;
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%mechanic_settlement}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['mechanic_id', 'status', 'payment_id', 'operator_id', 'payment_time', 'type', 'created_at','paymant_operator_id','updated_at'], 'integer'],
            [['payment_id', 'paymant_operator_id', 'status'], 'required'],
            [['commission_money','subsidy_money','additional_money','total'], 'number'],
            [['payment_method','picture'], 'string', 'max' => 20],
            [['operator','paymant_operator','year','month'], 'string', 'max' => 50],
            [['pic_url'], 'string', 'max' => 255],
        ];
    }

    public function fields()
    {
        $fields = parent::fields();

        $fields['created_at'] = function () {
            return \Yii::$app->formatter->asDatetime($this->created_at);
        };
        $fields['payment_time'] = function () {
            if($this->payment_time > 0)
                return \Yii::$app->formatter->asDatetime($this->payment_time);
            else
                return \Yii::t('zhtfrontend', '--');
        };
        return $fields;
    }

    public function extraFields()
    {
        return [
            'pay_status_name' => function () {
                switch ($this->pay_status) {
                    case 0:
                        return \Yii::t('zhtfrontend', '未付款');
                    case 1:
                        return \Yii::t('zhtfrontend', '已付款');
                }
            },
            'mechanic_name' => function(){
                return isset($this->mechanic->name)?$this->mechanic->name:'';
            },
            'process' =>function(){
                $item = ReportRecord::find()->where(['module_id'=>$this->id,'type'=>2])->orderBy('id DESC')->one();
                if($item != null){
                    if($item->stage == 10){
                        return \Yii::t('zhtfrontend', '教务提报');
                    }elseif ($item->stage == 11 && $item->status == 1){
                        return \Yii::t('zhtfrontend', '教服已通过');
                    }elseif ($item->stage == 11 && $item->status == 2){
                        return \Yii::t('zhtfrontend', '教服已驳回');
                    }elseif ($item->stage == 12 && $item->status == 1){
                        return \Yii::t('zhtfrontend', '机构负责人已通过');
                    }elseif ($item->stage == 12 && $item->status == 2){
                        return \Yii::t('zhtfrontend', '机构负责人已驳回');
                    }elseif ($item->stage == 13 && $item->status == 1){
                        return \Yii::t('zhtfrontend', '财务已通过');
                    }elseif ($item->stage == 13 && $item->status == 2){
                        return \Yii::t('zhtfrontend', '财务已驳回');
                    }elseif ($item->stage == 14){
                        return \Yii::t('zhtfrontend', '财务已付款');
                    }
                }
            },
            'operate' => function () {
                return (new MenuManage(MenuManage::RECORD_ITEM_TOOLBAR, $this->attributes))->isDisable(
                    [
                      'state' => function(){
                          $row = ReportRecord::find()->where(['module_id'=>$this->id,'type'=>2])->orderBy('id DESC')->one();
                            if($row != null){
                                if($row->stage == 13 && $row->status == 1 && $this->pay_status == 0){
                                    return '';
                                }else{
                                    return '<button class="yx-btn-disabled" type="button">结算</button>';
                                }
                            }else{
                                return '<button class="yx-btn-disabled" type="button">结算</button>';
                            }
                      },
                       'verify' =>function(){
                           if(\Yii::$app->user->identity->role == 'class_director'){
                               $row = ReportRecord::find()->where(['module_id'=>$this->id,'type'=>2])->orderBy('id DESC')->one();
                               if($row != null){
                                   if($row->stage == 10){
                                       return '';
                                   }else{
                                       return '<button class="yx-btn-disabled" type="button">核实数据</button>';
                                   }
                               }else{
                                   return '<button class="yx-btn-disabled" type="button">核实数据</button>';
                               }
                           }elseif (\Yii::$app->user->identity->role == 'finance'){
                               $row = ReportRecord::find()->where(['module_id'=>$this->id,'type'=>2])->orderBy('id DESC')->one();
                               if($row != null){
                                   if($row->stage == 12 && $row->status == 1){
                                       return '';
                                   }else{
                                       return '<button class="yx-btn-disabled" type="button">核实数据</button>';
                                   }
                               }else{
                                   return '<button class="yx-btn-disabled" type="button">核实数据</button>';
                               }
                           }else{
                               return '<button class="yx-btn-disabled" type="button">核实数据</button>';
                           }
                       },
                       'set' =>function(){
                           if(\Yii::$app->user->identity->role == 'class_director'){
                               $row = ReportRecord::find()->where(['module_id'=>$this->id,'type'=>2])->orderBy('id DESC')->one();
                               if($row != null){
                                   if($row->stage == 10){
                                       return '';
                                   }else{
                                       return '<button class="yx-btn-disabled" type="button">设置奖惩</button>';
                                   }
                               }else{
                                   return '<button class="yx-btn-disabled" type="button">设置奖惩</button>';
                               }
                           }else{
                               return '<button  class="yx-btn-disabled" type="button">设置奖惩</button>';
                           }
                       }

                    ]
                )->create();
            },
            'mechanicoperate'=>function(){
                return (new MenuManage(MenuManage::RECORD_ITEM_TOOLBAR, $this->attributes))->isDisable(
                    [
                        'verify' =>function(){
                            $row = ReportRecord::find()->where(['module_id'=>$this->id,'type'=>2])->orderBy('id DESC')->one();
                            if($row != null){
                                if($row->stage == 11 && $row->status == 1){
                                    return '';
                                }else{
                                    return '<button class="yx-btn-disabled" type="button">核实数据</button>';
                                }
                            }else{
                                return '<button class="yx-btn-disabled" type="button">核实数据</button>';
                            }

                        },
                        'view' =>function(){
                           return '';
                        },
                        'classfire'=>function(){
                            return '';
                        }

                    ]
                )->create();
            }
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('zhtfrontend', '自增id'),
            'mechanic_id' => Yii::t('zhtfrontend', '机构id'),
            'status' => Yii::t('zhtfrontend', '付款状态'),
            'commission_money' => Yii::t('zhtfrontend', '分成金'),
            'subsidy_money' => Yii::t('zhtfrontend', '补贴金'),
            'additional_money' => Yii::t('zhtfrontend', '额外补贴或扣除'),
            'total' => Yii::t('zhtfrontend', '总金额'),
            'payment_method' => Yii::t('zhtfrontend', '付款方式'),
            'payment_id' => Yii::t('zhtfrontend', '付款方式'),
            'operator' => Yii::t('zhtfrontend', '操作人'),
            'paymant_operator' => Yii::t('zhtfrontend', '付款人'),
            'operator_id' => Yii::t('zhtfrontend', '操作人id'),
            'paymant_operator_id' => Yii::t('zhtfrontend', '付款人'),
            'payment_time' => Yii::t('zhtfrontend', '付款时间'),
            'type' => Yii::t('zhtfrontend', '1 补贴 2 分成'),
            'created_at' => Yii::t('zhtfrontend', '创建时间'),
            'pic_url' => Yii::t('zhtfrontend', '付款凭证'),
            'picture' => Yii::t('zhtfrontend', ''),
            'year' => Yii::t('zhtfrontend', '年份'),
            'month' => Yii::t('zhtfrontend', '月份'),
            'updated_at' => Yii::t('zhtfrontend', '更新时间'),
        ];
    }

    public function  getMechanic()
    {
        return $this->hasOne(Mechanic::className(), ['id' => 'mechanic_id']);
    }


    public function behaviors()
    {
        return ArrayHelper::merge(parent::behaviors(), [
            [
                'class' => TimestampBehavior::className(),
                'attributes' => [
                    ActiveRecord::EVENT_BEFORE_INSERT => ['created_at', 'updated_at'],
                    ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at'],
                ],
            ],
        ]);
    }
}
